package com.shinhye.mobilelottery.ui.manager;

import java.util.Observable;
import java.util.Observer;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.GlobalParams;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.ui.UserLoginUI;

public class TitleManager implements Observer{
	private TitleManager(){}
	private static TitleManager instance = new TitleManager();
	public static TitleManager getInstance(){
		return instance;
	}
	
	private final String TAG = "TitleManager";
	
	private RelativeLayout commonContainer;
	private RelativeLayout unLoginContainer;
	private RelativeLayout loginContainer;
	private ImageView login;
	private TextView title;
	private TextView userInfo;

	public void init(Activity main) {
		commonContainer = (RelativeLayout) main.findViewById(R.id.ii_common_container);
		unLoginContainer = (RelativeLayout) main.findViewById(R.id.ii_unlogin_title);
		loginContainer = (RelativeLayout) main.findViewById(R.id.ii_login_title);
		
		login = (ImageView) main.findViewById(R.id.ii_title_login);
		title = (TextView) main.findViewById(R.id.ii_title_content);
		userInfo = (TextView) main.findViewById(R.id.ii_top_user_info);
		
		setListener();
	}

	private void setListener() {
		login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(TAG, "登录");
//				MiddleManager.getInstance().changeUI(new SecondUI(MiddleManager.getInstance().getContext()));
//				MiddleManager.getInstance().changeUI(SecondUI.class, null);
				MiddleManager.getInstance().changeUI(UserLoginUI.class, null);
			}
		});
	}
	
	/**
	 * 显示通用标题
	 */
	public void showCommonTitle() {
		initTitle();
		commonContainer.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 显示登录标题
	 */
	public void showLoginTitle() {
		initTitle();
		loginContainer.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示未登录标题
	 */
	public void showUnLoginTitle() {
		initTitle();
		unLoginContainer.setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏所有标题
	 */
	private void initTitle() {
		commonContainer.setVisibility(View.GONE);
		loginContainer.setVisibility(View.GONE);
		unLoginContainer.setVisibility(View.GONE);
	}

	/**
	 * 改变标题栏内容
	 * @param newtitle
	 */
	public void changeTitle(String newtitle) {
		title.setText(newtitle);
	}

	@Override
	public void update(Observable observable, Object data) {
		if(data != null && StringUtils.isNumeric(data.toString())) {
			int id = Integer.parseInt(data.toString());
			switch(id) {
			case ConstantValue.VIEW_FIRST:
				showUnLoginTitle();
				break;
			case ConstantValue.VIEW_PLAYSSQ:
			case ConstantValue.VIEW_SECOND:
			case ConstantValue.VIEW_SHOPPING:
			case ConstantValue.VIEW_USERLOGIN:
			case ConstantValue.VIEW_PREBET:
				showCommonTitle();
				break;
			case ConstantValue.VIEW_HALL:
				if(GlobalParams.isLogin) {
					String text="你好："+GlobalParams.username+"\r\n"+"余额："+GlobalParams.money;
					showLoginTitle();
					userInfo.setText(text);
				} else {
					showUnLoginTitle();
				}
				break;
			}
		}
	}
}
